package com.youchuang.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.youchuang.bean.Indent;
import com.youchuang.common.HibernateConnection;
import com.youchuang.dao.IndentDAO;

@Repository("indentDAO")
public class IndentDAOImpl implements IndentDAO {

	private static final String HQL_FIND_ALL = "from Indent";
	private static final String HQL_FIND_BY_ID = "from Indent where id = :id";
	private static final String HQL_FIND_BY_ID_LIMMIT_DESC = "from Indent where userId = :userId order by id desc";

	@Resource
	private HibernateConnection<Indent> hibernateConnection;

	@Override
	public void save(Indent t) {
		hibernateConnection.save(t);
	}

	@Override
	public void delete(Indent t) {
		hibernateConnection.delete(t);
	}

	@Override
	public int update(Indent t) {
		hibernateConnection.update(t);
		return 1;
	}

	@Override
	public List<Indent> queryAll() {
		return hibernateConnection.find(HQL_FIND_ALL);
	}

	@Override
	public List<Indent> queryLimit(int first, int max) {
		return hibernateConnection.find(HQL_FIND_ALL, first, max);
	}

	@Override
	public Indent queryById(String id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		List<Indent> list = hibernateConnection.find(HQL_FIND_BY_ID, params);
		if (CollectionUtils.isEmpty(list))
			return null;
		return list.get(0);
	}

	@Override
	public List<Indent> queryByUserIdDESC(String userId) {
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		return hibernateConnection.find(HQL_FIND_BY_ID_LIMMIT_DESC, params);
	}

}
